/*
 * 文件名：InnerSettleController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年2月24日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import top.infopub.mgr.bpa.domain.InnerSettleAssist;
import top.infopub.mgr.bpa.domain.InnerSettleDomain;
import top.infopub.mgr.bpa.service.*;
import top.infopub.mgr.bpa.vo.InnerSettleVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.util.FileHelper;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.bpa.InnerSettleAtt;
import top.infopub.model.bpa.InnerSettleDetail;
import top.infopub.model.bpa.InnerSettleList;
import top.infopub.model.bpa.InnerSettleSub;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;


/**结算管理
 * @author wuzhiyang
 * @version 2020年2月24日
 * @see InnerSettleController
 * @since商务管理/结算管理/对内结算
 */
@Controller
@RequestMapping("/innerSettle")
public class InnerSettleController extends GenericFlowController {

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private InnerSettleService innerSettleService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private InnerAgrtService innerAgrtService;

    @Autowired
    private SubpackCotService subpackCotService;

    @Autowired
    private OtherCotService otherCotService;

    @Autowired
    private DeviceCotService deviceCotService;

    @Autowired
    private InnerAgrtDetailService innerAgrtDetailService;

    @Autowired
    private SubpackCotDetailService subpackCotDetailService;

    @Autowired
    private SubpackCotListService subpackCotListService;

    @Autowired
    private DeviceCotPriceService deviceCotPriceService;

    @Autowired
    private OtherCotListService otherCotListService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private InnerSettleDetailService innerSettleDetailService;

    @Autowired
    private InnerSettleListService innerSettleListService;

    /**
     * 对内结算列表
     * @return 
     * @see
     */
    @RequestMapping("/list")
    public String list() {
        return "bpa/lay-innerSettle-list";
    }

    /**
     * 对内结算列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<InnerSettleDomain> queryDataByCondition(InnerSettleVo innerSettleVo,
                                                         ParamPage paramPage) {
        Page<InnerSettleDomain> page = new Page<InnerSettleDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (innerSettleVo.getEndDate() != null) {
            innerSettleVo.setEndDate(DateUtils.addDays(innerSettleVo.getEndDate(), 1));
        }
        page = innerSettleService.selectByDomainWhere(page, innerSettleVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 对内结算-我审批的列表数据
     *
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<InnerSettleDomain> queryMyCheckDataByCondition(InnerSettleVo innerSettleVo,
                                                                ParamPage paramPage) {
        Page<InnerSettleDomain> page = new Page<InnerSettleDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (innerSettleVo.getEndDate() != null) {
            innerSettleVo.setEndDate(DateUtils.addDays(innerSettleVo.getEndDate(), 1));
        }
        innerSettleVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = innerSettleService.selectMyCheckByDomainWhere(page, innerSettleVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 对内结算-新增
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(HttpServletRequest request) {
        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
        request.setAttribute("bindOrgId", bindOrgId);
        request.setAttribute("bindOrgName", bindOrgName);
        request.setAttribute("bindDeptId", bindDeptId);
        request.setAttribute("bindDeptName", bindDeptName);
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        return "bpa/lay-innerSettle-add";
    }

    /**
     * 对内结算-新增
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(InnerSettleAssist innerSettleAssist) {

        //主表-对内结算
        innerSettleAssist.setHrDeptId(innerSettleAssist.getHrDeptid());
        innerSettleAssist.setHrOrgId(innerSettleAssist.getHrOrgid());
        innerSettleAssist.setStaffName(innerSettleAssist.getStaffname());
        innerSettleAssist.setHrStaffApplyId(innerSettleAssist.getHrStaffApplyid());
        String proCode = "";
        String code = "";
        String[] strCodes = innerSettleAssist.getProCodeAndCodes().split("\\_");
        proCode = strCodes[0];
        code = strCodes[1];
        innerSettleAssist.setProCode(proCode);
        //父关联id
        innerSettleAssist.setRefId(code);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String id = IdFactory.getUUIDUpper();
        //添加项目名称
        innerSettleAssist.setProName(strCodes[2]);

        innerSettleAssist.setId(id);
        innerSettleAssist.setAddBy(vo.getId());
        innerSettleAssist.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String codes = codeGeneratorConfigService.generatorCode("bpa_inner_settle");
        innerSettleAssist.setCodes(codes);
        // 处理processId
        innerSettleAssist.setProcessId(IdFactory.getUUIDUpper());
        innerSettleAssist.setProcessStatus("1");//审批中
        innerSettleAssist.setTypes("1");//PPP项目
        innerSettleAssist.setUptBy(vo.getId());
        innerSettleAssist.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        //新增分解子项
        List<InnerSettleSub> innerSettleSubs = new ArrayList<InnerSettleSub>();
        //分解子项费用明细
        List<InnerSettleDetail> innerSettleDetails = new ArrayList<InnerSettleDetail>();
        //清单模式
        List<InnerSettleList> innerSettleLists = new ArrayList<InnerSettleList>();

        if ("1".equals(innerSettleAssist.getSettleType())) {
            //子项名称
            String[] subNames = innerSettleAssist.getSubNames();
            if (subNames != null && subNames.length > 0) {
                for (int i = 0; i < subNames.length; i++ ) {
                    String subName = subNames[i];
                    if (StringUtils.isEmpty(subName) && StringUtils.isEmpty(subName)) {
                        continue;
                    }
                    InnerSettleSub innerSettleSub = new InnerSettleSub();
                    String innerSettleSubId = IdFactory.getUUIDUpper();//自动生成主键
                    innerSettleSub.setId(innerSettleSubId);
                    innerSettleSub.setInnerSettleId(id);
                    innerSettleSub.setSubName(subName);
                    innerSettleSub.setAddBy(vo.getId());
                    Date addTime = top.infopub.mgr.util.DateUtils.addSeconds(
                        top.infopub.mgr.util.DateUtils.getCurrentDateTime(), i);
                    innerSettleSub.setAddTime(addTime);
                    innerSettleSubs.add(innerSettleSub);
                }

                //费用大类
                String[] orderIdxs = innerSettleAssist.getOrderIdxs();
                for (int j = 0; j < orderIdxs.length; j++ ) {
                    String orderIdx = orderIdxs[j];
                    String itemNameSub = innerSettleAssist.getItemNameSub()[j];
                    String typeName = innerSettleAssist.getTypeNames()[j];
                    String afterDownFee = innerSettleAssist.getAfterDownFees()[j];
                    String applyPreAmount = innerSettleAssist.getApplyPreAmounts()[j];
                    String verifyPreAmount = innerSettleAssist.getVerifyPreAmounts()[j];
                    String innerDownRate = innerSettleAssist.getInnerDownRates()[j];
                    String verifyAfterAmount = innerSettleAssist.getVerifyAfterAmounts()[j];
                    String addupVerifyAmount = innerSettleAssist.getAddupVerifyAmounts()[j];
                    String restVerifyAmount = innerSettleAssist.getRestVerifyAmounts()[j];
                    if (StringUtils.isEmpty(orderIdx) && StringUtils.isEmpty(orderIdx)) {
                        continue;
                    }
                    InnerSettleDetail innerSettleDetail = new InnerSettleDetail();
                    String innerSettleDetailId = IdFactory.getUUIDUpper();//自动生成主键
                    innerSettleDetail.setId(innerSettleDetailId);

                    innerSettleDetail.setSettleSubId(itemNameSub);

                    innerSettleDetail.setInnerSettleId(id);
                    innerSettleDetail.setOrderIdx(orderIdx);
                    innerSettleDetail.setTypeName(typeName);
                    innerSettleDetail.setAfterDownFee(new BigDecimal(afterDownFee));
                    innerSettleDetail.setApplyPreAmount(new BigDecimal(applyPreAmount));
                    innerSettleDetail.setVerifyPreAmount(new BigDecimal(verifyPreAmount));
                    innerSettleDetail.setInnerDownRate(new BigDecimal(innerDownRate));
                    innerSettleDetail.setVerifyAfterAmount(new BigDecimal(verifyAfterAmount));
                    innerSettleDetail.setAddupVerifyAmount(new BigDecimal(addupVerifyAmount));
                    innerSettleDetail.setRestVerifyAmount(new BigDecimal(restVerifyAmount));
                    innerSettleDetail.setAddBy(vo.getId());
                    innerSettleDetail.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
                    innerSettleDetails.add(innerSettleDetail);
                }

                for (InnerSettleDetail innerSettleDetail : innerSettleDetails) {
                    for (InnerSettleSub innerSettleSub : innerSettleSubs) {
                        if (innerSettleDetail.getSettleSubId().equals(innerSettleSub.getSubName())) {
                            innerSettleDetail.setSettleSubId(innerSettleSub.getId());
                            break;
                        }
                    }
                }
            }
        }
        else if ("2".equals(innerSettleAssist.getSettleType())) {
            //清单
            String[] showIdxLists = innerSettleAssist.getShowIdxLists();
            if (showIdxLists != null && showIdxLists.length > 0) {
                for (int j = 0; j < showIdxLists.length; j++ ) {
                    String showIdxList = showIdxLists[j];
                    String subNameList = innerSettleAssist.getSubNameLists()[j];
                    String subUnitList = innerSettleAssist.getSubUnitLists()[j];
                    String cotNumList = innerSettleAssist.getCotNumLists()[j];
                    String cotUnitPriceList = innerSettleAssist.getCotUnitPriceLists()[j];
                    String cotSumPriceList = innerSettleAssist.getCotSumPriceLists()[j];
                    String applyNumList = innerSettleAssist.getApplyNumLists()[j];
                    String applyAmountList = innerSettleAssist.getApplyAmountLists()[j];
                    String verifyNumList = innerSettleAssist.getVerifyNumLists()[j];
                    String verifyAmountList = innerSettleAssist.getVerifyAmountLists()[j];
                    String addupVerifyAmountList = innerSettleAssist.getAddupVerifyAmountLists()[j];
                    String restVerifyAmountList = innerSettleAssist.getRestVerifyAmountLists()[j];

                    InnerSettleList innerSettleList = new InnerSettleList();
                    String innerSettleListId = IdFactory.getUUIDUpper();//自动生成主键
                    innerSettleList.setId(innerSettleListId);
                    innerSettleList.setInnerSettleId(id);
                    innerSettleList.setShowIdx(Integer.parseInt(showIdxList));
                    innerSettleList.setSubName(subNameList);
                    innerSettleList.setSubUnit(subUnitList);
                    innerSettleList.setCotNums(new BigDecimal(cotNumList));
                    innerSettleList.setCotUnitPrice(new BigDecimal(cotUnitPriceList));
                    innerSettleList.setCotSumPrice(new BigDecimal(cotSumPriceList));
                    innerSettleList.setApplyNums(new BigDecimal(applyNumList));
                    innerSettleList.setApplyAmount(new BigDecimal(applyAmountList));
                    innerSettleList.setVerifyNums(new BigDecimal(verifyNumList));
                    innerSettleList.setVerifyAmount(new BigDecimal(verifyAmountList));
                    innerSettleList.setAddupVerifyAmount(new BigDecimal(addupVerifyAmountList));
                    innerSettleList.setRestVerifyAmount(new BigDecimal(restVerifyAmountList));
                    innerSettleList.setAddBy(vo.getId());
                    innerSettleList.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
                    innerSettleLists.add(innerSettleList);
                }
            }
        }

        //上传附件
        //1=计算稿
        List<InnerSettleAtt> innerSettleAtts = new ArrayList<InnerSettleAtt>();
        String[] fileAtt1 = innerSettleAssist.getFileAtts1();
        if (fileAtt1 != null) {
            for (String file : fileAtt1) {
                InnerSettleAtt innerSettleAtt = createFileAtt(vo, id, file);
                innerSettleAtt.setTypes("1");
                innerSettleAtts.add(innerSettleAtt);
            }
        }
        //2=签字扫描件
        String[] fileAtt2 = innerSettleAssist.getFileAtts2();
        if (fileAtt2 != null) {
            for (String file : fileAtt2) {
                InnerSettleAtt innerSettleAtt = createFileAtt(vo, id, file);
                innerSettleAtt.setTypes("2");
                innerSettleAtts.add(innerSettleAtt);
            }
        }

        //2=其他附件（选填）
        String[] fileAtt3 = innerSettleAssist.getFileAtts3();
        if (fileAtt3 != null) {
            for (String file : fileAtt3) {
                InnerSettleAtt innerSettleAtt = createFileAtt(vo, id, file);
                innerSettleAtt.setTypes("3");
                innerSettleAtts.add(innerSettleAtt);
            }
        }
        innerSettleService.saveInnerSettle(innerSettleAssist, innerSettleLists, innerSettleSubs,
            innerSettleDetails, innerSettleAtts);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "商务管理 -结算管理-对内结算：项目名="
                                                              + innerSettleAssist.getProName(),
            null);
        return RspVo.getSuccessResponse();
    }

    private InnerSettleAtt createFileAtt(AuthorizationVO vo, String innerSettleId, String file) {
        //资料信息数组
        String[] fileAttStr = file.split("\\|");
        InnerSettleAtt fileAtt = new InnerSettleAtt();
        if (StringUtils.isNotEmpty(fileAttStr[2])) {
            fileAtt.setId(fileAttStr[2]);
        }
        else {
            fileAtt.setId(IdFactory.getUUIDSerialNumber());
        }
        fileAtt.setInnerSettleId(innerSettleId);//项目id
        String fileName = fileAttStr[1].substring(0, fileAttStr[1].lastIndexOf("."));
        String suffix = fileAttStr[1].substring(fileAttStr[1].lastIndexOf("."),
            fileAttStr[1].length()).toLowerCase();//后缀转小写
        fileAtt.setFileName(fileName);//文件名
        fileAtt.setFileSuffix(suffix);//文件后缀
        fileAtt.setFilePath(fileAttStr[3]);//文件保存路径
        fileAtt.setAddBy(vo.getId());//添加人
        fileAtt.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());//添加时间
        return fileAtt;
    }

    /**
     * 上传附件
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/uploadAtt")
    @ResponseBody
    public RspVo updateAtt(HttpServletRequest request) {
        List<String> returnFileStrList = new ArrayList<>();
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
        if (fileMap != null) {
            for (String name : fileMap.keySet()) {
                MultipartFile file = fileMap.get(name);
                //资料类型
                String types = request.getParameter("types");
                if (StringUtils.isEmpty(types)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
                }
                //上传文件
                String fileId = IdFactory.getUUIDSerialNumber();
                //服务器资源路径
                String serverPath = "innerSettle";
                String filePath = FileHelper.upload(file, serverPath);
                returnFileStrList.add("|" + file.getOriginalFilename().trim() + "|" + fileId + "|"
                                      + filePath);
                LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "上传结算管理-对内结算附件-文件名:"
                                                                      + filePath + "，文件路径:"
                                                                      + filePath, null);
            }
        }
        return RspVo.getSuccessResponse(returnFileStrList);
    }

    /**
     * 根据项目合同中项目编号和合同类型查询相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
//    @RequestMapping("/getInnerSettleByProCode")
//    @ResponseBody
//    public Map<String, Object> getInnerSettleByProCode(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        String proCodes = request.getParameter("proCodeAndCodes");
//        String[] proCode = proCodes.split("\\_");
//        String extType = request.getParameter("extType");
//        try {
//            Map<String, Object> mapWhere = new HashMap<String, Object>();
//            mapWhere.put("refId", proCode[1]);
//            mapWhere.put("processStatus", "9");
//            mapWhere.put("orderCondition", "ADD_TIME desc");
//            if ("1".equals(extType)) {
//                InnerAgrtDomain innerAgrtDomain = new InnerAgrtDomain();
//                List<InnerAgrt> innerAgrts = innerAgrtService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(innerAgrts)) {
//                    innerAgrtDomain = innerAgrtService.selectByIds(innerAgrts.get(0).getId());
//                }
//                else {
//                    innerAgrtDomain = innerAgrtService.selectByIds(proCode[1]);
//                }
//                if (innerAgrtDomain != null) {
//                    map.put("innerAgrtDomain", innerAgrtDomain);
//                    List<InnerAgrtDetailAndSubDomain> byInnerAgrtIdList = innerAgrtDetailService.selectByInnerAgrtId(innerAgrtDomain.getId());
//                    if (CollectionUtils.isNotEmpty(byInnerAgrtIdList)) {
//                        for (InnerAgrtDetailAndSubDomain iAndSubDomain : byInnerAgrtIdList) {
//                            Map<String, Object> where = Maps.newHashMap();
//                            where.put("typeName", iAndSubDomain.getTypeName());
//                            where.put("subName", iAndSubDomain.getSubName());
//                            where.put("refId", proCode[1]);
//                            //前期已结算金额
//                            BigDecimal vfAmount = innerSettleDetailService.selectVfAmount(where);
//                            iAndSubDomain.setAddupVfAmountDetail(vfAmount);
//                            //对内合同价(下浮后)
//                            BigDecimal afterDownFee = iAndSubDomain.getAfterDownFee();
//                            //剩余可结算金额
//                            BigDecimal restVfAmountDetail = afterDownFee.subtract(vfAmount);
//                            iAndSubDomain.setRestVfAmountDetail(restVfAmountDetail);
//                        }
//                        map.put("innerAgrtDetailAndSubList", byInnerAgrtIdList);
//                    }
//                }
//                else {
//                    flag = false;
//                }
//                //期数
//                int countByName = innerAgrtService.countByName(proCode[3]);
//                map.put("countByName", countByName);
//            }
//            else if ("2".equals(extType)) {
//                SubpackCotDomain subpackCotDomain = new SubpackCotDomain();
//                List<SubpackCot> subpackCots = subpackCotService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(subpackCots)) {
//                    subpackCotDomain = subpackCotService.selectById(subpackCots.get(0).getId());
//                }
//                else {
//                    subpackCotDomain = subpackCotService.selectById(proCode[1]);
//                }
//                if (subpackCotDomain != null) {
//                    map.put("subpackCotDomain", subpackCotDomain);
//                    //带出 合同清单子项  和  合同清单子项费用明细  信息
//                    List<SubpackCotDetailAndSubDomain> subpackCotIdList = subpackCotDetailService.selectBySubpackCotId(subpackCotDomain.getId());
//                    if (CollectionUtils.isNotEmpty(subpackCotIdList)) {
//                        for (SubpackCotDetailAndSubDomain iAndSubDomain : subpackCotIdList) {
//                            Map<String, Object> where = Maps.newHashMap();
//                            where.put("typeName", iAndSubDomain.getTypeName());
//                            where.put("subName", iAndSubDomain.getSubName());
//                            where.put("refId", proCode[1]);
//                            //前期已结算金额
//                            BigDecimal vfAmount = innerSettleDetailService.selectVfAmount(where);
//                            iAndSubDomain.setAddupVfAmountDetail(vfAmount);
//                            //对内合同价(下浮后)
//                            BigDecimal afterDownFee = iAndSubDomain.getAfterDownFee();
//                            //剩余可结算金额
//                            BigDecimal restVfAmountDetail = afterDownFee.subtract(vfAmount);
//                            iAndSubDomain.setRestVfAmountDetail(restVfAmountDetail);
//                        }
//                        map.put("subpackCotDetailAndSubList", subpackCotIdList);
//                    }
//                    //带出清单模式
//                    List<SubpackCotListDomain> subpackCotLists = subpackCotListService.selectBySubpackCotId(subpackCotDomain.getId());
//                    if (CollectionUtils.isNotEmpty(subpackCotLists)) {
//                        for (SubpackCotListDomain iAndSubDomain : subpackCotLists) {
//                            Map<String, Object> where = Maps.newHashMap();
//                            where.put("subName", iAndSubDomain.getSubName());
//                            where.put("refId", proCode[1]);
//                            //前期已结算金额
//                            BigDecimal vfAmount = innerSettleListService.selectVfAmountList(where);
//                            iAndSubDomain.setAddupVfAmountList(vfAmount);
//                            //合价
//                            BigDecimal cotSumPrice = iAndSubDomain.getCotSumPrice();
//                            //剩余可结算金额
//                            BigDecimal restVfAmountDetail = cotSumPrice.subtract(vfAmount);
//                            iAndSubDomain.setRestVfAmountList(restVfAmountDetail);
//                        }
//                        map.put("subpackCotList", subpackCotLists);
//                    }
//                }
//                else {
//                    flag = false;
//                }
//                //期数
//                int countByName = subpackCotService.countByName(proCode[3]);
//                map.put("countByName", countByName);
//            }
//            else if ("3".equals(extType)) {
//                DeviceCotVo deviceCotVo = new DeviceCotVo();
//                List<DeviceCot> deviceCots = deviceCotService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(deviceCots)) {
//                    deviceCotVo = deviceCotService.queryById(deviceCots.get(0).getId());
//                }
//                else {
//                    deviceCotVo = deviceCotService.queryById(proCode[1]);
//                }
//                if (deviceCotVo != null) {
//                    map.put("deviceCotVo", deviceCotVo);
//                    //带出清单模式
//                    List<DeviceCotPriceVo> deviceCotLists = deviceCotPriceService.selectByDeviceCotId(deviceCotVo.getId());
//                    if (CollectionUtils.isNotEmpty(deviceCotLists)) {
//                        for (DeviceCotPriceVo iAndSubDomain : deviceCotLists) {
//                            Map<String, Object> where = Maps.newHashMap();
//                            where.put("subName", iAndSubDomain.getSubName());
//                            where.put("refId", proCode[1]);
//                            //前期已结算金额
//                            BigDecimal vfAmount = innerSettleListService.selectVfAmountList(where);
//                            iAndSubDomain.setAddupVfAmountList(vfAmount);
//                            //合价
//                            BigDecimal cotSumPrice = iAndSubDomain.getCotSumPrice();
//                            //剩余可结算金额
//                            BigDecimal restVfAmountDetail = cotSumPrice.subtract(vfAmount);
//                            iAndSubDomain.setRestVfAmountList(restVfAmountDetail);
//                        }
//                        map.put("deviceCotList", deviceCotLists);
//                    }
//                }
//                else {
//                    flag = false;
//                }
//                //期数
//                int countByName = deviceCotService.countByName(proCode[3]);
//                map.put("countByName", countByName);
//            }
//            else if ("4".equals(extType)) {
//                OtherCotDomain otherCotDomain = new OtherCotDomain();
//                List<OtherCot> otherCots = otherCotService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(otherCots)) {
//                    otherCotDomain = otherCotService.selectByIds(otherCots.get(0).getId());
//                }
//                else {
//                    otherCotDomain = otherCotService.selectByIds(proCode[1]);
//                }
//                if (otherCotDomain != null) {
//                    map.put("otherCotDomain", otherCotDomain);
//                    //清单模式
//                    List<OtherCotListVo> selectOtherCotLists = otherCotListService.selectOtherCotListVo(otherCotDomain.getId());
//                    if (CollectionUtils.isNotEmpty(selectOtherCotLists)) {
//                        for (OtherCotListVo iAndSubDomain : selectOtherCotLists) {
//                            Map<String, Object> where = Maps.newHashMap();
//                            where.put("subName", iAndSubDomain.getSubName());
//                            where.put("refId", proCode[1]);
//                            //前期已结算金额
//                            BigDecimal vfAmount = innerSettleListService.selectVfAmountList(where);
//                            iAndSubDomain.setAddupVfAmountList(vfAmount);
//                            //合价
//                            BigDecimal cotSumPrice = iAndSubDomain.getCotSumPrice();
//                            //剩余可结算金额
//                            BigDecimal restVfAmountDetail = cotSumPrice.subtract(vfAmount);
//                            iAndSubDomain.setRestVfAmountList(restVfAmountDetail);
//                        }
//                        map.put("selectOtherCotList", selectOtherCotLists);
//                    }
//                }
//                else {
//                    flag = false;
//                }
//                //期数
//                int countByName = otherCotService.countByName(proCode[3]);
//                map.put("countByName", countByName);
//            }
//        }
//        catch (Exception e) {
//            log.error("查询记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

    /**
     * 根据合同类型相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
//    @RequestMapping("/getInnerSettleName")
//    @ResponseBody
//    public Map<String, Object> getInnerSettleName(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        try {
//            String types = request.getParameter("extType");
//            //合同类型
//            if ("1".equals(types)) {
//                //返回所有内部协议中的项目名称
//                List<InnerAgrt> innerAgrtList = innerAgrtService.selectInnerAgrt();
//                map.put("InnerAgrtList", innerAgrtList);
//            }
//            else if ("2".equals(types)) {
//                //返回分包合同中的项目名称
//                List<SubpackCot> selectSubpackCotList = subpackCotService.selectSubpackCot();
//                map.put("subpackCotList", selectSubpackCotList);
//            }
//            else if ("3".equals(types)) {
//                //返回所有设备合同的项目名称
//                List<DeviceCot> deviceCotList = deviceCotService.selectByDeviceCots();
//                map.put("deviceCotList", deviceCotList);
//            }
//            else if ("4".equals(types)) {
//                //返回所有其他费合同的项目名称
//                List<OtherCot> otherCotList = otherCotService.selectByOtherCots();
//                map.put("otherCotList", otherCotList);
//            }
//        }
//        catch (Exception e) {
//            log.error("查询记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

    /**
     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 分包合同id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
        Map<String, Object> map = new HashMap<String, Object>();
        InnerSettleVo innerSettleVo = innerSettleService.queryById(id);
        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setProjectCode(innerSettleVo.getProCode());
        orgInfo.setState(1);
        List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
        if (orgInfos.size() > 0) {
            innerSettleVo.setOrgProName(orgInfos.get(0).getCompName());
            innerSettleVo.setHrOrgProId(orgInfos.get(0).getId());
            OrgInfo parentOrgInfo = new OrgInfo();
            parentOrgInfo.setId(orgInfos.get(0).getParentId());
            parentOrgInfo.setState(1);
            List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
            if (parentOInfos.size() > 0) {
                innerSettleVo.setOrgAreName(parentOInfos.get(0).getCompName());
                innerSettleVo.setHrOrgAreaId(parentOInfos.get(0).getId());
            }
        }
        map.put("innerSettleVo", innerSettleVo);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, innerSettleVo.getProcessId());
        return new ModelAndView("bpa/lay-innerSettle-detail", map);
    }

    /**
     * 对内结算-复制单据
     * @return
     */
    @RequestMapping("/toCopy")
    public String toCopy(HttpServletRequest request) {
        String id = request.getParameter("id");
        InnerSettleVo innerSettleVo = innerSettleService.queryById(id);
        request.setAttribute("innerSettleVo", innerSettleVo);
        return "bpa/lay-innerSettle-copy";
    }

    /**
     * 根据对内结算id查询相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getInnerSettleById")
    @ResponseBody
    public Map<String, Object> getInnerSettleById(HttpServletRequest request) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        String sguid = request.getParameter("sguid");
        try {
            InnerSettleVo innerSettleVo = innerSettleService.queryById(sguid);
            map.put("innerSettleVo", innerSettleVo);
        }
        catch (Exception e) {
            log.error("查询对内结算记录异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }
}
